#!/usr/bin/php
<?php
/*
 * Copyright (C) 2013-2014 RuneAudio Team
 * http://www.runeaudio.com
 *
 * RuneUI
 * copyright (C) 2013-2014 - Andrea Coiutti (aka ACX) & Simone De Gregori (aka Orion)
 *
 * RuneOS
 * copyright (C) 2013-2014 - Simone De Gregori (aka Orion) & Carmelo San Giovanni (aka Um3ggh1U)
 *
 * RuneAudio website and logo
 * copyright (C) 2013-2014 - ACX webdesign (Andrea Coiutti)
 *
 * This Program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This Program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with RuneAudio; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.txt>.
 *
 *  file: db/redis_datastore_setup
 *  version: 1.3
 *  coder: Simone De Gregori
 *
 */
// common include
include('/srv/http/app/config/config.php');
// ini_set('display_errors',1);
// error_reporting('E_ALL');
function redisDatastore($redis,$action) {
    switch ($action) {
        case 'reset':
            // kernel
            $redis->set('kernel', 'linux-arch-rpi_3.12.26-1-ARCH');
            $redis->set('i2smodule', 'none');
            $redis->set('orionprofile', 'RuneAudio');
            $redis->set('activePlayer', 'MPD');
            // player features
            $redis->set('hostname', 'runeaudio');
            $redis->set('ntpserver', 'pool.ntp.org');
            $redis->hSet('airplay', 'enable', 1);
            $redis->hSet('airplay', 'name', 'RuneAudio');
            $redis->hSet('dlna', 'enable', 0);
            $redis->hSet('dlna', 'name', 'RuneAudio');
            $redis->hSet('dlna', 'queue', 1);
            $redis->hSet('dlna', 'logfile', '/var/log/runeaudio/upmpdcli.log');
            $redis->hSet('dlna', 'loglevel', 6);
            $redis->set('udevil', 1);
            $redis->hSet('spotify', 'enable', 0);
            $redis->hSet('spotify', 'user', 'user');
            $redis->hSet('spotify', 'pass', 'pass'); 
            $redis->hSet('spotify', 'plversion', 1);
            $redis->hSet('spotify', 'lastcmd', '');            
            $redis->set('coverart', 1);
            $redis->set('playmod', 0);
            $redis->set('ramplay', 0);
            $redis->hSet('lastfm', 'enable', 0);
            $redis->hSet('lastfm', 'user', 'user');
            $redis->hSet('lastfm', 'pass', 'pass');
            $redis->set('cmediafix', 0);
            $redis->set('globalrandom', 0);
            $redis->set('stoppedPlayer', '');    
            // default MPD config
            $redis->hSet('mpdconf', 'zeroconf_enabled', 'yes');
            $redis->hSet('mpdconf', 'zeroconf_name', 'RuneAudio');
            $redis->hSet('mpdconf', 'log_level', 'none');
            $redis->hSet('mpdconf', 'bind_to_address', 'any');
            $redis->hSet('mpdconf', 'port', '6600');
            $redis->hSet('mpdconf', 'max_connections', '20');
            $redis->hSet('mpdconf', 'user', 'mpd');
            $redis->hSet('mpdconf', 'db_file', '/var/lib/mpd/mpd.db');
            $redis->hSet('mpdconf', 'sticker_file', '/var/lib/mpd/sticker.sql');
            $redis->hSet('mpdconf', 'pid_file', '/var/run/mpd/pid');
            $redis->hSet('mpdconf', 'music_directory', '/mnt/MPD');
            $redis->hSet('mpdconf', 'playlist_directory', '/var/lib/mpd/playlists');
            $redis->hSet('mpdconf', 'state_file', '/var/lib/mpd/mpdstate');
            $redis->hSet('mpdconf', 'follow_outside_symlinks', 'yes');
            $redis->hSet('mpdconf', 'follow_inside_symlinks', 'yes');
            $redis->hSet('mpdconf', 'auto_update', 'no');
            $redis->hSet('mpdconf', 'filesystem_charset', 'UTF-8');
            $redis->hSet('mpdconf', 'id3v1_encoding', 'UTF-8');
            $redis->hSet('mpdconf', 'volume_normalization', 'no');
            $redis->hSet('mpdconf', 'audio_buffer_size', '2048');
            $redis->hSet('mpdconf', 'buffer_before_play', '10%');
            $redis->hSet('mpdconf', 'gapless_mp3_playback', 'yes');
            $redis->hSet('mpdconf', 'mixer_type', 'software');
            $redis->hSet('mpdconf', 'curl', 'yes');
            $redis->hSet('mpdconf', 'ffmpeg', 'yes');
            $redis->hSet('mpdconf', 'log_file', '/var/log/runeaudio/mpd.log');
            // plugins api-keys
            $redis->set('lastfm_apikey', 'ba8ad00468a50732a3860832eaed0882');
            $redis->hSet('lastfm', 'apikey', 'ba8ad00468a50732a3860832eaed0882');
            $redis->hSet('spotify', 'clientid', 'c236e0f5a84e48a3a7f6c730f684f255');
            $redis->hSet('jamendo', 'clientid', '5f3ed86c');
            $redis->hSet('jamendo', 'secret', '1afcdcb13eb5ce8f6e534ac4566a3ab9');
            $redis->hSet('dirble', 'apikey', '134aabbce2878ce0dbfdb23fb3b46265eded085b');
            $redis->hSet('dirble', 'baseurl', 'http://api.dirble.com/v1/');
            // internal config hash control
            // $redis->set('mpdconfhash', '');
            // $redis->set('netconfhash', '');
            $redis->set('mpdconfhash', '12eed229f02c52816ed997cbce4b9f32');
            $redis->hSet('netconfhash', 'eth0', '643f8967af551f683b3cfd493950c550');
            $redis->set('mpdconf_advanced', 0);
            $redis->set('netconf_advanced', 0);
            // MPD parameters
            wrk_mpdconf($redis, 'writecfg');
            $redis->set('mpd_playback_status', '');
            // developer parameters
            $redis->set('dev', 0);
            $redis->set('debug', 0);
            $redis->set('debugdata', '');
            $redis->set('opcache', 1);
            $redis->set('buildversion', 'beta-20141027');
            // HW platform data
            // $redis->set('playerid', '');
            // $redis->set('hwplatform', '');
            // $redis->set('hwplatformid', '');
            // player control
            if ($redis->get('hwplatformid') === '01') {
                $redis->set('ao', 'bcm2835 ALSA_1');
            } else {
                $redis->set('ao', '');
            }
            $redis->set('volume', 0);
            $redis->set('pl_length', 0);
            $redis->set('nextsongid', 0);
            $redis->set('lastsongid', 0);
            // network parameters
            $redis->hSet('resolvconf', 'timeout', '0.3');
            $redis->hSet('resolvconf', 'attempts', '1');
            // proxy settings
            $redis->hSet('proxy', 'host', '');
            $redis->hSet('proxy', 'user', '');
            $redis->hSet('proxy', 'pass', '');
            // lock keys
            $redis->set('lock_globalrandom', 0);
            $redis->set('lock_refresh_ao', 0);
            $redis->set('lock_wifiscan', 0);
            // env settings
            $redis->del('usbmounts');
            $redis->set('wlans', '');
            $redis->del('acards');
            $redis->set('mountidx', 1);
            $redis->set('debugdata', '');
            // PHP precache (prime) scripts
            $redis->del('php_opcache_prime');
            $transaction = $redis->multi();
            // RuneAudio Libs & config
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/config/config.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/runeaudio.php');
            // RuneAudio Controllers
            $transaction->sAdd('php_opcache_prime', '/srv/http/index.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/db/index.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/index.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/coverart_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/credits_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/debug_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/dev_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/login_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/mpd_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/network_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/playback_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/settings_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/sources_ctl.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/tun_ctl.php');
            // RuneAudio Templates
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/credits.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/debug.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/default_lo.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/dev.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/error.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/footer.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/header.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/login.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/mpd.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/mpd_manual.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/network.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/network_edit.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/network_edit_manual.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/network_wlan.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/playback.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/settings.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/sources.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/templates/sources_edit.php');
            // Vendor libs
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/autoload.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Engine.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Template.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Extension/Escape.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Extension/Batch.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Extension/ExtensionInterface.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Extension/Asset.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/league/plates/src/Extension/URI.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/autoload_namespaces.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/autoload_files.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/autoload_real.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/ClassLoader.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/autoload_classmap.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/composer/autoload_psr4.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.tag.id3v2.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/audioinfo.class.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.tag.id3v1.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/getid3.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.audio.ogg.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.tag.apetag.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/getid3.lib.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.audio.flac.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/getid3/module.tag.lyrics3.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/app/libs/vendor/react/promise/src/React/Promise/functions.php');
            // RuneAudio Commands
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/refresh_nics');
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/ui_notify.php');
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/debug_collector');
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/opcache.php');
            // RuneAudio Workers
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/rune_PL_wrk');
            $transaction->sAdd('php_opcache_prime', '/srv/http/command/rune_SY_wrk');
            $transaction->exec();
            sysCmd('redis-cli KEYS "mount_*" | xargs redis-cli DEL');
            break;
        case 'check':
            // kernel
            $redis->get('kernel') || $redis->set('kernel', 'linux-arch-rpi_3.12.26-1-ARCH');
            $redis->get('i2smodule') || $redis->set('i2smodule', 'none');
            $redis->get('orionprofile') || $redis->set('orionprofile', 'RuneAudio');
            $redis->get('activePlayer') || $redis->set('activePlayer', 'MPD');
            // player features
            $redis->get('hostname') || $redis->set('hostname', 'runeaudio');
            $redis->get('ntpserver') || $redis->set('ntpserver', 'pool.ntp.org');
            $redis->hGet('airplay', 'enable') || $redis->hSet('airplay', 'enable', 1);
            $redis->hGet('airplay', 'name') || $redis->hSet('airplay', 'name', 'RuneAudio');        
            $redis->hGet('dlna', 'enable') || $redis->hSet('dlna', 'enable', 0);
            $redis->hGet('dlna', 'name') || $redis->hSet('dlna', 'name', 'RuneAudio');
            $redis->hGet('dlna', 'queue') || $redis->hSet('dlna', 'queue', 1);
            $redis->hGet('dlna', 'logfile') || $redis->hSet('dlna', 'logfile', '/var/log/runeaudio/upmpdcli.log');
            $redis->hGet('dlna', 'loglevel') || $redis->hSet('dlna', 'loglevel', 0);
            $redis->hGet('spotify', 'enable') || $redis->hSet('spotify', 'enable', 0);
            $redis->hGet('spotify', 'user') || $redis->hSet('spotify', 'user', 'user');
            $redis->hGet('spotify', 'pass') || $redis->hSet('spotify', 'pass', 'pass');
            $redis->hGet('spotify', 'plversion') || $redis->hSet('spotify', 'plversion', 1);
            $redis->hGet('spotify', 'lastcmd') || $redis->hSet('spotify', 'lastcmd', '');
            $redis->get('udevil') || $redis->set('udevil', 1);
            $redis->get('coverart') || $redis->set('coverart', 1);
            $redis->get('playmod') || $redis->set('playmod', 0);
            $redis->get('ramplay') || $redis->set('ramplay', 0);
            $redis->hGet('lastfm', 'enable') || $redis->hSet('lastfm', 'enable', 0);
            $redis->hGet('lastfm', 'username') || $redis->hSet('lastfm', 'username', '');
            $redis->hGet('lastfm', 'password') || $redis->hSet('lastfm', 'password', '');
            $redis->get('cmediafix') || $redis->set('cmediafix', 0);
            $redis->get('globalrandom') || $redis->set('globalrandom', 0);
            $redis->get('stoppedPlayer') || $redis->set('stoppedPlayer', '');
            // plugins api-keys
            $redis->get('lastfm_apikey') || $redis->set('lastfm_apikey', 'ba8ad00468a50732a3860832eaed0882');
            $redis->hGet('lastfm', 'apikey') || $redis->hSet('lastfm', 'apikey', 'ba8ad00468a50732a3860832eaed0882');
            $redis->hGet('jamendo', 'clientid') || $redis->hSet('jamendo', 'clientid', '5f3ed86c');
            $redis->hGet('jamendo', 'secret') || $redis->hSet('jamendo', 'secret', '1afcdcb13eb5ce8f6e534ac4566a3ab9');
            $redis->hGet('dirble','apikey') || $redis->hSet('dirble', 'apikey', '134aabbce2878ce0dbfdb23fb3b46265eded085b');
            // internal config hash control
            $redis->get('mpdconfhash') || $redis->set('mpdconfhash', '12eed229f02c52816ed997cbce4b9f32');
            $redis->hGet('netconfhash') || $redis->hSet('netconfhash', 'eth0', '643f8967af551f683b3cfd493950c550');
            $redis->get('mpdconf_advanced') || $redis->set('mpdconf_advanced', 0);
            $redis->get('netconf_advanced') || $redis->set('netconf_advanced', 0);
            // MPD parameters
            $redis->get('mpd_playback_status') || $redis->set('mpd_playback_status', '');
            // developer parameters
            $redis->get('dev') || $redis->set('dev', 0);
            $redis->get('debug') || $redis->set('debug', 0);
            $redis->get('debugdata') || $redis->set('debugdata', '');
            $redis->get('opcache') || $redis->set('opcache', 1);
            $redis->get('buildversion') || $redis->set('buildversion', 'alpha-20141002');
            // HW platform data
            $redis->get('playerid') || $redis->set('playerid', '');
            $redis->get('hwplatform') || $redis->set('hwplatform', '');
            $redis->get('hwplatformid') || $redis->set('hwplatformid', '');
            // player control
            $redis->get('ao') || $redis->set('ao', 1);
            $redis->get('volume') || $redis->set('volume', 0);
            $redis->get('pl_length') || $redis->set('pl_length', 0);
            $redis->get('nextsongid') || $redis->set('nextsongid', 0);
            $redis->get('lastsongid') || $redis->set('lastsongid', 0);
            // network parameters
            $redis->hGet('resolvconf', 'timeout') || $redis->hSet('resolvconf', 'timeout', '0.3');
            $redis->hGet('resolvconf', 'attempts') || $redis->hSet('resolvconf', 'attempts', '1');
            // lock keys
            $redis->get('lock_globalrandom') || $redis->set('globalrandom_lock', 0);
            $redis->get('lock_refresh_ao') || $redis->set('lock_refresh_ao', 0);
            $redis->get('lock_wifiscan') || $redis->set('lock_wifiscan', 0);
            break;
    }
}
// inspect console command
if (isset($argv[1])) {
    switch ($argv[1]) {
        case 'reset':
            redisDatastore($redis,'reset');
            sysCmd('/srv/http/db/redis_acards_details');
            break;
        case 'check':
            redisDatastore($redis,'check');
            break;
    }
} else {
    echo "RuneAudio Datastore reset\nredis_datastore_setup {reset|check}\n";
}
